module bag {
    export class BagManager implements IBagInternal, IBagPublic {
        private _bagData: BagData;

        constructor(bagData: BagData) {
            this._bagData = bagData;
        }

        setCapacity(capacity: number): void {
            this._bagData.capacity = capacity;
        }
        setOccupy(occupy: boolean): void {
            this._bagData.autoOccupy = occupy;
        }
        setPile(pile: boolean): void {
            this._bagData.autoPile = pile;
        }

        removePropByCount(pos: number, count: number): { prop: IBagPropData; count: number; } {
            throw new Error("Method not implemented.");
        }
        removeAllProp(pos: number): { prop: IBagPropData; count: number; } {
            throw new Error("Method not implemented.");
        }
        exist(conditions: { id: number; count: number; }[]): boolean {
            throw new Error("Method not implemented.");
        }

        isFill(): boolean {
            let index = this.getEmptyIndex();
            return index === -1;
        }

        isPileFill(propId: number): boolean {
            let index = this.getEmptyPileIndex(propId);
            return index === -1;
        }

        /**
         * 获得空的格子索引
         * @returns 数字小于零说明没有空间，否则就是索引值
         */
        getEmptyIndex(): number {
            const max = this._bagData.capacity;
            for (let i = 0; i < max; i++) {
                if (i >= this._bagData.list.length) {
                    return i;
                }
                let _unitData = this._bagData.list[i];
                if (_unitData.count === 0) {
                    return i;
                }
            }
            return -1;
        }

        /**
         * 获得还未堆叠满的道具id
         * @param propId 道具id
         */
        getEmptyPileIndex(propId: number): number {
            const max = this._bagData.capacity;
            for (let i = 0; i < max; i++) {
                // 找到最后一个新的格子
                if (i >= this._bagData.list.length) {
                    return i;
                }
                let _unitData = this._bagData.list[i];
                // 找到空的位置也行
                if (_unitData.count === 0) {
                    return i;
                }
                // 找到到这个道具，并且堆叠没有满，则返回这个位置
                if (_unitData.prop.getConfigId() === propId && _unitData.prop.getConfigPile() < _unitData.count) {
                    return i;
                }
            }
            return -1;
        }
        /**
         * 重置预设
         */
        resetPrimary() {
            this._bagData.list.forEach(e => e.choose = PrimaryType.UNCHOOSE);
        }
        addProp(items: { prop: IBagPropData; count: number; }[]): { pos: number; prop: { total: number; }; }[] {
            throw new Error("Method not implemented.");
        }
        sort(sortType: number): void {
            throw new Error("Method not implemented.");
        }
        list(pageIndex: number, pageSize: number, type: number): BagUnitData[] {

            throw new Error("Method not implemented.");
        }
        /**
         * 预选中
         * @param pos 位置
         */
        primary(pos: number): { success: boolean, unitData: BagUnitData } {
            // 没有选中格子
            if (pos < this._bagData.list.length) {
                return { success: false, unitData: null };
            }

            let unitData = this._bagData.list[pos];
            // 选中的格子是空的
            if (unitData.count === 0) {
                return { success: false, unitData };
            }

            // 该格子不是未选中状态
            if (unitData.choose !== PrimaryType.UNCHOOSE) {
                return { success: false, unitData };
            }

            unitData.choose = PrimaryType.CHOOSE;
            return { success: true, unitData };
        }

        cancelPrimary(pos: number): boolean {
            let bagUnitData = this._bagData.list[pos];
            if (bagUnitData.choose === PrimaryType.CHOOSE) {
                bagUnitData.choose = PrimaryType.UNCHOOSE;
                return true;
            }
            return false;
        }
        getPrimaryList(): BagUnitData[] {
            let primaryList = this._bagData.list.filter(e => !(e.choose === PrimaryType.UNCHOOSE));
            return primaryList;
        }
        lockPrimary(pos: number): boolean {
            let bagUnitData = this._bagData.list[pos];
            if (bagUnitData.choose !== PrimaryType.LOCK) {
                bagUnitData.choose = PrimaryType.LOCK;
                return true;
            }
            return false;
        }
        unlockPrimary(pos: number): boolean {
            let bagUnitData = this._bagData.list[pos];
            if (bagUnitData.choose === PrimaryType.LOCK) {
                bagUnitData.choose = PrimaryType.UNCHOOSE;
                return true;
            }
            return false;
        }

    }
}